package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.workflow.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaProjectBo;
import com.ruoyi.oa.domain.vo.OaProjectVo;
import com.ruoyi.oa.domain.OaProject;
import com.ruoyi.oa.mapper.OaProjectMapper;
import com.ruoyi.oa.service.IOaProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-21
 */
@RequiredArgsConstructor
@Service
public class OaProjectServiceImpl implements IOaProjectService {

    private final OaProjectMapper baseMapper;

    private final IUserService iUserService;
    /**
     * 查询项目
     */
    @Override
    public OaProjectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }


    @Override
    public TableDataInfo<OaProjectVo> queryPageList(OaProjectBo bo, PageQuery pageQuery){
        if (StringUtils.isNotBlank(bo.getCreateBy())){
            String userName = iUserService.getUserNameByNickName(bo.getCreateBy());
            if (StringUtils.isNotBlank(userName)){
                bo.setCreateBy(userName);
            }
        }
        List<OaProjectVo> voList = baseMapper.queryByCondition(bo);
        //构造page
        Page<OaProjectVo> result = new Page<>();
        result.setTotal(voList.size());
        result.setRecords(voList);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目列表
     */
    @Override
    public List<OaProjectVo> queryList(OaProjectBo bo) {
        String userName = iUserService.getUserNameByNickName(bo.getCreateBy());
        if (StringUtils.isNotBlank(userName)){
            bo.setProjectName(userName);
        }
        LambdaQueryWrapper<OaProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaProject> buildQueryWrapper(OaProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), OaProject::getProjectName, bo.getProjectName());
        lqw.eq(bo.getStartDate() != null, OaProject::getStartDate, bo.getStartDate());
        return lqw;
    }

    /**
     * 新增项目
     */
    @Override
    public R<Void> insertByBo(OaProjectBo bo) {
        String result = this.checkUserNameUnique(bo);
        if (result.equals("0")){
            LoginUser currentUser = LoginHelper.getLoginUser();
            bo.setDeptId(currentUser.getDeptId());
            bo.setUserId(currentUser.getUserId());
            OaProject add = BeanUtil.toBean(bo, OaProject.class);
            validEntityBeforeSave(add);
            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setId(add.getId());
            }
            return R.ok();
        }
        return R.fail("新增项目失败，项目："+bo.getProjectName()+"已存在！");
    }

    public String checkUserNameUnique(OaProjectBo bo) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<OaProject>()
            .eq(OaProject::getProjectName, bo.getProjectName()));
        if (exist) {
            return Constants.FAIL;
        }
        return Constants.SUCCESS;
    }

    /**
     * 修改项目
     */
    @Override
    public Boolean updateByBo(OaProjectBo bo) {
        OaProject update = BeanUtil.toBean(bo, OaProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取所有项目信息
     * @return
     */
    @Override
    public List<OaProject> getProjectName() {
        return baseMapper.selectList();
    }
}
